<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/js/themes/default/easyui.css">   
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/js/themes/icon.css">  

<script src="<%=request.getContextPath() %>/js/jquery-1.11.2.min.js"></script> 
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery.easyui.min.js"></script> 
</head>
<body>
 <table id="da"></table> 
 <div id="dia" class="easyui-dialog" title="用户权限管理" style="width:400px;height:400px;"   
        data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true,buttons:'#bt'">   
        
        <span id="roleInfo" style="color: red;font-size: 20px"> </span>
       	<ul id="trees"></ul>
        
       
</div> 
<!-- dialog的两个按钮 -->
 	<div id="bt">
			  <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'"   
        onclick="addRole_power()">添加</a>  
        	  <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'"   
        onclick="remove()">取消</a> 
		</div> 
<!-- 用于添加角色的 -->	
<div id="dial" class="easyui-dialog" title="用户权限管理" style="width:400px;height:400px;"   
        data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true,buttons:'#btt'">   
        <form id="forms">
        	角色名称：<input type="text" name="rname"><input type="hidden" id="rid">
        	<input type="submit" value="提交">
        </form> 
</div> 
<!-- dialog的两个按钮 -->
 	<div id="btt">
			  <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'"   
        onclick="addRole_power()">添加</a>  
        	  <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'"   
        onclick="remove()">取消</a> 
		</div> 
	
 
 <script type="text/javascript">
 	$("#da").datagrid({
 		 url:'<%=request.getContextPath() %>/role/getRoles',    
 	    columns:[[    
 	        {field:'rid',title:'角色ID',width:100},    
 	        {field:'rname',title:'角色名称',width:100},  
 	        {field:'xx',title:'操作',width:100,formatter: function(value,row,index){
 				return "<input type='button' onClick='power("+row.rid+")' value='角色赋权限'>";
 			}
 	},
 	    ]],
 	    fitColumns:true,
 	    singleSelect:true,
 	   toolbar: [{
 			iconCls: 'icon-add',
 			handler: function(){
 				//角色的添加
 				$("#dial").dialog('open');

 				 $('#forms').form({    
 	   				 url:"<%=request.getContextPath() %>/role/addAdminRole",    
 	  				  onSubmit: function(){    
 	  					 //首先加载，返回验证
 	  					  return $("#forms").form('validate');    
 			   		 },    
 			    success:function(obj){    
 			    	if(obj){
 			    		//加强版弹框
 			    		if(obj==1){
 			    			$.messager.alert("系统提示","保存成功");
 			    			//dialog的关闭
 							$("#dial").dialog("close");
 							//datagrid的刷新
 							$("#da").datagrid("reload");
 			    		}
 						
 					}else{
 						$.messager.alert("系统提示","保存失败");
 					}    
 			    }    
 			});    
 				
 				
 			
 			}
 		},'-',{
 			iconCls: 'icon-remove',
 			handler: function(){
 				//实现删除功能
 				//获取datagrid的一行的信息
 				var row = $("#da").datagrid("getSelected");
 				//ajax执行删除
 				$.ajax({
 					url:"<%=request.getContextPath() %>/role/removeRoleByRid",
 					type:"post",
 					data:{rid:row.rid},
 					success:function(obj){
 						alert(obj);
 						if(obj==1){
 							$.messager.alert("系统提示","删除成功");
 							//datagrid的刷新
 							$("#da").datagrid("reload");
 						}
 					},
 					dataType:"text",	
 				});
 				
 			
 			}
 		}]

 	});
 	
 	
 	function power(rid){
 		//打开一个dialog
 		$("#dia").dialog('open');
 		
 		//追加角色的信息
 		$.ajax({
 			url:"<%=request.getContextPath() %>/role/getRoleByRid",
 			type:"post",
 			data:{rid:rid},
 			success:function(obj){	
 				
 				$("#roleInfo").empty();
 				$("#roleInfo").html("当前用户："+obj.rname+"<br><input type='hidden' id='userId' value='"+obj.rid+"'/>");
 				$("#rid").val(rid);
 			},
 			dataType:"json",
 		});
 		//权限树的展示
 		$("#trees").tree({
			url:"<%=request.getContextPath() %>/power/getPower",
			idFiled:"id",
	  		textFiled:"name",
	  		parentField:"pid",
			checkbox:true,
			lines:true,
			cascadeCheck:false,
			onClick:function(node){
				if($("#trees").tree('isLeaf',node.target)){
					alert(node.id);	
					//先回显树节点
					
				}
			},
			//数据加载成功后执行
			onLoadSuccess:function(node){
				$.ajax({
					url:"<%=request.getContextPath() %>/role/getRolePowerListByRid",
					type:"post",
					data:{rid:rid},
					success:function(obj){
						//alert(obj);
						if(obj!=null){

							for ( var i in obj) {
								//alert(obj[i].pid);
								var node = $('#trees').tree('find', obj[i].pid);
								$('#trees').tree('check', node.target);

							}
						}
						
						
					},
					dataType:"json"
				});
			}
			
 	})
 	
 	
 	
 	
 	
 	}
 	
 	//添加角色权限表
 	function addRole_power(){
 		
 		//获取角色id
 		var rid=$("#userId").val();
 		
 		
 		//获取权限的数组
 		//获取选中的树节点
 		var nodes=$("#trees").tree('getChecked');
		var ids="";
		var idArray=[];
		for ( var i in nodes) {
			idArray.push(nodes[i].id);
		}
		ids=idArray.join();
		//修改角色权限表
		$.ajax({
			url:"<%=request.getContextPath() %>/role/updateRolePower",
			type:"post",
			data:{rid:rid,pids:ids},
			success:function(obj){
				alert("赋权成功");
				if(obj==1){					
				$("#dia").dialog('close');
				}
			},
			dataType:"text",
			
			
		});
		
 	}
 
 
 </script>
 
</body>
</html>